﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using SignalRDemo.Hubs;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;

namespace SignalRDemo.Controllers
{
    public class TestController : Controller
    {
        private IHubContext<ChatHub> _chatHub;

        public TestController(IHubContext<ChatHub> chatHub)
        {
            _chatHub = chatHub;
        }

        public async Task<IActionResult> Chat()
        {
            var dic = ChatHub.OnLineUsers.FirstOrDefault();
            await _chatHub.Clients.Client(dic.Value).SendAsync("ReceiveMessage", "server", JsonSerializer.Serialize(new { rtnCode = 0, rtnMsg = "Chat Chat Chat" }));
            return View();
        }
    }
}
